import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '朋友圈效果',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MomentsPage(),
    );
  }
}

class MomentsPage extends StatefulWidget {
  @override
  _MomentsPageState createState() => _MomentsPageState();
}

class _MomentsPageState extends State<MomentsPage> {
  final ScrollController _scrollController = ScrollController();
  double _imageHeight = 300.0;
  bool isDown = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      setState(() {
        if (_scrollController.offset < 0) {
          _imageHeight = 300.0 - _scrollController.offset;
        } else {
          _imageHeight = 300.0;
        }
      });
    });
  }

  Widget _header() {
    return Positioned(
      left: 20.0,
      top: 20.0,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          // 用户头像
          Container(
            width: 80.0,
            height: 80.0,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.0),
              border: Border.all(
                color: Colors.white,
                width: 2.0,
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.black26,
                  blurRadius: 6.0,
                  offset: Offset(0, 2),
                ),
              ],
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(6.0),
              child: Image.asset(
                'assets/my.jpg',
                fit: BoxFit.cover,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[100],
        body: ListView(
            scrollDirection: Axis.vertical,
            padding: EdgeInsets.only(top: 0),
            children: <Widget>[
              Stack(children: <Widget>[
                LayoutBuilder(builder:
                    (BuildContext context, BoxConstraints constraints) {
                  return GestureDetector(
                      onTap: () {
                        // 点击事件的处理
                        if (this.isDown) {
                          SystemChrome.setPreferredOrientations([
                            DeviceOrientation.portraitUp,
                            DeviceOrientation.portraitDown,
                            DeviceOrientation.landscapeLeft,
                            DeviceOrientation.landscapeRight,
                          ]);
                        } else {
                          SystemChrome.setPreferredOrientations([
                            DeviceOrientation.landscapeLeft,
                            DeviceOrientation.landscapeRight,
                          ]);
                        }
                        this.isDown = !this.isDown;
                      },
                      child: Container(
                        width: constraints.maxWidth, // 使用父容器的最大宽度
                        height: 260, // 设定高度
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage(
                                'assets/background.png'), // 替换为你的背景图片路径
                            fit: BoxFit.fill,
                          ),
                        ),
                        child: Column(
                          children: [],
                        ),
                      ));
                }),
                Container(
                  height: 280,
                ),
                Positioned(
                  right: 30,
                  bottom: 0,
                  child: Container(
                    height: 50,
                    width: 50,
                    child: Image.asset('assets/my.jpg'),
                  ),
                ),
              ]),
              Divider(
                color: Colors.grey,
                thickness: 1.0,
                indent: 100.0,
                endIndent: 100.0,
              ),
              // 更多子项...
            ]));
  }

  Widget _buildMomentItem(int index) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
      padding: EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12.0),
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            blurRadius: 4.0,
            offset: Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              CircleAvatar(
                radius: 20.0,
                backgroundImage: AssetImage(
                  'assets/background.jpg',
                ),
              ),
              SizedBox(width: 12.0),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '用户${index + 1}',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16.0,
                      ),
                    ),
                    Text(
                      '${index + 1}小时前',
                      style: TextStyle(
                        color: Colors.grey,
                        fontSize: 12.0,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: 12.0),
          Text(
            '这是第${index + 1}条朋友圈内容，展示了Flutter实现的优雅UI效果。',
            style: TextStyle(fontSize: 14.0),
          ),
          SizedBox(height: 12.0),
          if (index % 3 == 0)
            ClipRRect(
              borderRadius: BorderRadius.circular(8.0),
              child: Image.asset(
                'assets/background.jpg',
                width: double.infinity,
                height: 200.0,
                fit: BoxFit.fill,
              ),
            ),
          SizedBox(height: 12.0),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Icon(Icons.favorite_border, size: 18.0, color: Colors.grey),
                  SizedBox(width: 4.0),
                  Text('点赞'),
                ],
              ),
              Row(
                children: [
                  Icon(Icons.chat_bubble_outline,
                      size: 18.0, color: Colors.grey),
                  SizedBox(width: 4.0),
                  Text('评论'),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }
}
